package wxy.studentmanage.handler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import wxy.studentmanage.bean.Users;
import wxy.studentmanage.service.UserService;

import java.util.List;
//StudController
@Controller()
@RequestMapping("/allUsers")
public class UserController {
    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;

    @RequestMapping("/allUser")
    public String allUser(Model model){
        List<Users> list = userService.queryAllUser();
        model.addAttribute("list", list);
        System.out.println(list);
        System.out.println(list);
        return "allUser";
    }

    //新增用户页面
    @RequestMapping("/toAddUser")
    public String toAddPaper() {
        return "addUser";
    }
    //添加User
    @RequestMapping("/addUser")
    public String addUser(Users users){
        System.out.println(users);
        userService.addUser(users);
        return "redirect:/allUsers/allUser";
    }
    //删除User
    @RequestMapping("/del/{userId}")
    public String deleteBook(@PathVariable("userId") int wxy_id) {
        System.out.println(wxy_id);
        int i = userService.deleteUserById(wxy_id);
        System.out.println(i);
        return "redirect:/allUsers/allUser";
    }
    //更改查询
    @RequestMapping("/toUpdateUser/{userId}")
    public String toUpdateUser(@PathVariable("userId") int wxy_id,Model model){
        System.out.println(wxy_id);
        Users users = userService.queryUserById(wxy_id);
        System.out.println(users);
        model.addAttribute("users",users);
        return "updateUser";
    }
    //更改更改
    @RequestMapping("/updateUser")
    public String updateBook(Model model,Users users){
        System.out.println(users);
        userService.updateUser(users);
        Users users1 = userService.queryUserById(users.getWxy_id());
        System.out.println(users1);
        return "redirect:/allUsers/allUser";
    }

}
